package zjc.bookkeeping.tools;

import androidx.annotation.NonNull;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import zjc.bookkeeping.R;

public class KeyboardDialog extends Dialog implements View.OnClickListener {
    TextView moneyTv;
    Button button1,button2,button3,button4,button5,button6,button7,button8,button9,button0,pointBtn,deleteBtn,clearBtn,sureBtn;

    public interface OnKeyboardSureListener{
        public void onKeyboardSure();
    }
    OnKeyboardSureListener onKeyboardSureListener;

    public void setOnKeyboardSureListener(OnKeyboardSureListener onKeyboardSureListener) {
        this.onKeyboardSureListener = onKeyboardSureListener;
    }

    public KeyboardDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_keyboard);
        initView();
    }

    private void initView() {
        moneyTv = findViewById(R.id.keyboard_tv_money);
        button1 = findViewById(R.id.keyboard_btn_1);
        button2 = findViewById(R.id.keyboard_btn_2);
        button3 = findViewById(R.id.keyboard_btn_3);
        button4 = findViewById(R.id.keyboard_btn_4);
        button5 = findViewById(R.id.keyboard_btn_5);
        button6 = findViewById(R.id.keyboard_btn_6);
        button7 = findViewById(R.id.keyboard_btn_7);
        button8 = findViewById(R.id.keyboard_btn_8);
        button9 = findViewById(R.id.keyboard_btn_9);
        button0 = findViewById(R.id.keyboard_btn_0);
        pointBtn = findViewById(R.id.keyboard_btn_point);
        deleteBtn = findViewById(R.id.keyboard_btn_delete);
        clearBtn = findViewById(R.id.keyboard_btn_clear);
        sureBtn = findViewById(R.id.keyboard_btn_sure);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        button8.setOnClickListener(this);
        button9.setOnClickListener(this);
        button0.setOnClickListener(this);
        pointBtn.setOnClickListener(this);
        clearBtn.setOnClickListener(this);
        deleteBtn.setOnClickListener(this);
        sureBtn.setOnClickListener(this);
    }

    private StringBuffer text = new StringBuffer("0");
    @Override
    public void onClick(View view) {
//        Log.i("StringBuffer",text.toString());
//        Log.i("textLength",text.length()+"wei");
        int textLength = text.length();
        switch (view.getId()){
            case R.id.keyboard_btn_0:
                if (!text.substring(0).equals("0")){
                    text.append('0');
                }
                break;
            case R.id.keyboard_btn_1:
                if (textLength == 1 && text.toString().contains("0")){
                    text.setCharAt(0,'1');
                }else {
                    text.append('1');
                }
                break;
            case R.id.keyboard_btn_2:
                if (textLength == 1 && text.toString().contains("0")){
                    text.setCharAt(0,'2');
                }else {
                    text.append('2');
                }
                break;
            case R.id.keyboard_btn_3:
                if (textLength == 1 && text.toString().contains("0")){
                    text.setCharAt(0,'3');
                }else {
                    text.append('3');
                }
                break;
            case R.id.keyboard_btn_4:
                if (textLength == 1 && text.toString().contains("0")){
                    text.setCharAt(0,'4');
                }else {
                    text.append('4');
                }
                break;
            case R.id.keyboard_btn_5:
                if (textLength == 1 && text.toString().contains("0")){
                    text.setCharAt(0,'5');
                }else {
                    text.append('5');
                }
                break;
            case R.id.keyboard_btn_6:
                if (textLength == 1 && text.toString().contains("0")){
                    text.setCharAt(0,'6');
                }else {
                    text.append('6');
                }
                break;
            case R.id.keyboard_btn_7:
                if (textLength == 1 && text.toString().contains("0")){
                    text.setCharAt(0,'7');
                }else {
                    text.append('7');
                }
                break;
            case R.id.keyboard_btn_8:
                if (textLength == 1 && text.toString().contains("0")){
                    text.setCharAt(0,'8');
                }else {
                    text.append('8');
                }
                break;
            case R.id.keyboard_btn_9:
                if (textLength == 1 && text.toString().contains("0")){
                    text.setCharAt(0,'9');
                }else {
                    text.append('9');
                }
                break;
            case R.id.keyboard_btn_point:
                if (!text.toString().contains(".")){
                    text.append('.');
                }
                break;
            case R.id.keyboard_btn_delete:
                if (textLength > 1){
                    text.deleteCharAt(textLength - 1);
                }
                if (textLength == 1){
                    text.setCharAt(0,'0');
                }
                break;
            case R.id.keyboard_btn_clear:
                text = new StringBuffer("0");
                break;
            case R.id.keyboard_btn_sure:
                if (onKeyboardSureListener != null){
                    onKeyboardSureListener.onKeyboardSure();
                }
                cancel();
                break;
        }
        String str = text.toString();
//        Log.i("money",str);
        moneyTv.setText(str);
    }
//    传递金额
    public String getMoneyTv(){
        return moneyTv.getText().toString().trim();
    }
    public void setKeyboardDialogSize(){
        Window window =getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        Display display = window.getWindowManager().getDefaultDisplay();
        layoutParams.width = (int) (display.getWidth());
        layoutParams.gravity = Gravity.BOTTOM;
        window.setAttributes(layoutParams);
    }
}